package com.tanhua.manager.controller;

import com.tanhua.manager.domain.Freeze;
import com.tanhua.manager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/manage")
public class UserController {
    @Autowired
    private UserService userService;


    /**
     * 用户数据翻页
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/users")
    public ResponseEntity findUserList(@RequestParam(defaultValue = "1") int page,
                                       @RequestParam(defaultValue = "10") int pagesize) {
        return userService.findUserList(page, pagesize);
    }

    /**
     * 用户冻结操作
     * POST/manage/users/freeze
     */
    @PostMapping("/users/freeze")
    public ResponseEntity freeze(@RequestBody Freeze freeze) {
        return userService.freeze(freeze);
    }


    /**
     * 用户解冻操作
     * POST/manage/users/unfreeze
     *
     * @param
     * @return
     */
    @PostMapping("/users/unfreeze")
    public ResponseEntity unfreeze(@RequestBody Map map) {
        Integer userId = Integer.parseInt(map.get("userId").toString());
        String reasonsForThawing = (String) map.get("reasonsForThawing");
        return userService.unfreeze(userId, reasonsForThawing);
    }


    /**
     * 用户基本资料
     * @param userId
     * @return
     */
    @GetMapping("/users/{id}")
    public ResponseEntity findUserById(@PathVariable("id") Long userId) {
        return userService.findUserById(userId);
    }

    @GetMapping("/messages")
    public ResponseEntity findPublishList(@RequestParam(defaultValue = "1") int page,
                                          @RequestParam(defaultValue = "10") int pagesize,
                                          @RequestParam(required = false) Long uid,
                                          @RequestParam(required = false) String state) {

        Long type = null;
        try {
            type = Long.parseLong(state);
        } catch (NumberFormatException e) {
        }
        return userService.findPublishList(page, pagesize, uid, type);
    }

    @GetMapping("/messages/{id}")
    public ResponseEntity findPublishById(@PathVariable("id") String publishId) {
        return userService.findPublishById(publishId);
    }

    @GetMapping("/messages/comments")
    public ResponseEntity findCommentList(@RequestParam(defaultValue = "1") int page,
                                          @RequestParam(defaultValue = "10") int pagesize,
                                          String messageID,
                                          String sortProp,
                                          String sortOrder) {
        return userService.findCommentList(page, pagesize, messageID);
    }

    @GetMapping("/videos")
    public ResponseEntity findVideoList(@RequestParam(defaultValue = "1") int page,
                                        @RequestParam(defaultValue = "10") int pagesize,
                                        Long uid) {
        return userService.findVideoList(page, pagesize, uid);
    }

    /**
     * 动态-动态拒绝
     * POST/manage/messages/reject
     */
    @PostMapping("/messages/reject")
    public ResponseEntity reject(@RequestBody String[] publishIdList) {
        return userService.reject(publishIdList);
    }

    /**
     * 动态-动态通过
     * POST/manage/messages/pass
     */
    @PostMapping("/messages/pass")
    public ResponseEntity pass(@RequestBody String[] publishIdList) {
        return userService.pass(publishIdList);
    }

}
